﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.DataAccess.Entities;
using CRM.Business.Services;
using CRM.Encryption;
using Telerik.Web.UI;
using CRM.DataAccess.DAOs;

namespace Web.Description_Details
{
    public partial class DescriptionDetails : System.Web.UI.Page
    {
        static List<DESCRIPTION_DETAILS> dds;
        static USER user;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            user = (USER)Session["User"];
             if (user != null)
             {
                 DateTime dt = DateTime.Now;
                 DateTime dt2 = dt.AddMonths(-3);
                 dpDateCreatedFrom.SelectedDate = new DateTime(dt2.Year, dt2.Month, 1);
                 dpDateCreatedTo.SelectedDate = DateTime.Now;
                 dds = bindGrid(dpDateCreatedFrom.SelectedDate, dpDateCreatedTo.SelectedDate.Value.AddHours(24), "");
                 RadGrid1.DataSource = dds;
                 RadGrid1.DataBind();
             }
             else
             {
                 RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessageAndRedirect('" + "Please login before continue !" + "','" + "Login.aspx" + "');");
             }
        }
        private List<DESCRIPTION_DETAILS> bindGrid(DateTime? dateFrom, DateTime? dateTo, string DescriptionName)
        {
            List<DESCRIPTION_DETAILS> dds2 = DescriptionDetailService.GetDescriptionByCondition(dateFrom, dateTo, DescriptionName);
            if (dds2 == null)
            {
                dds2 = new List<DESCRIPTION_DETAILS>();
            }
            return dds2;
        }
        protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            RadGrid1.DataSource = dds;
        }
        protected void btnSearch_ServerClick(object sender, EventArgs e)
        {
            dds = bindGrid(dpDateCreatedFrom.SelectedDate, dpDateCreatedTo.SelectedDate.Value.AddHours(24),txtDescriptionName.Text.Trim());
            RadGrid1.DataSource = dds;
            RadGrid1.DataBind();
        }
        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            GridDataItem dataItem = e.Item as GridDataItem;
            switch (e.CommandName)
            {
                case "Edit":
                    Response.Redirect("EditDescription.aspx?id=" + dataItem["ID"].Text);
                    break;
                case "Delete":
                    long ddID = Convert.ToInt64(dataItem["ID"].Text);
                    int deleteDescription = DescriptionDetailService.DeleteDescription(ddID);
                    if (deleteDescription == 1)
                    {
                        dds.Remove(dds.Where(x => x.ID == ddID).FirstOrDefault<DESCRIPTION_DETAILS>());
                        RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Delete Description successful !" + "');");
                        //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('Delete role successful !');", true);
                    }
                    else
                        RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Can not delete this Description !" + "');");
                    //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('Cannot delete this role !');", true);
                    break;
                default:
                    break;
            }

        }
        protected void btnAddDescription_ServerClick(object sender, EventArgs e)
        {
            Response.Redirect("AddNewDescription.aspx");
        }
        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridPagerItem)
            {
                GridPagerItem oGridPagerItem = (GridPagerItem)e.Item;
                RadComboBox oRadComboBox = (RadComboBox)oGridPagerItem.FindControl("PageSizeComboBox");
                oRadComboBox.Width = Unit.Pixel(50);
            }
        }

        protected void lbtnExportToCSV_ServerClick(object sender, EventArgs e)
        {
            RadGrid1.ExportSettings.IgnorePaging = true;
            RadGrid1.ExportSettings.OpenInNewWindow = true;
            RadGrid1.ExportSettings.UseItemStyles = true;
            DateTime dt = DateTime.Now;
            RadGrid1.ExportSettings.FileName = "Description Details" + dt.Year + dt.Month + dt.Day;
            RadGrid1.MasterTableView.GetColumn("TOOLS").Display = false;
            RadGrid1.MasterTableView.ExportToCSV();
            RadGrid1.Rebind();
        }

    }
}